using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Xml;
using Graffiti.Core;
using System.Web;

namespace Graffiti.Slide.Show
{
    /// <summary>
    /// Flickr Slide.Show Photo Gallery
    /// </summary>
    public class FlickrSlideShow2Plugin : BaseSlideShowPlugin
    {

        #region Properties

        private string _flickrUsername = string.Empty;
        public string FlickrUsername
        {
            get { return _flickrUsername; }
            set { _flickrUsername = value; }
        }

        private string _flickrApiKey = "b45329018eafdf2a4f89ebf6fb2bf47a";
        public string FlickrApiKey
        {
            get { return _flickrApiKey; }
            set { _flickrApiKey = value; }
        }

        #endregion

        #region Constructor
        public FlickrSlideShow2Plugin()
        {
        }
        #endregion

        #region GraffitiEvent Properties

        public override string Name
        {
            get { return "Flickr Slide.Show2 Photo Gallery"; }
        }

        public override string Description
        {
            get
            {
                return "Adds a photo gallery to a post page using Flickr and the Slide.Show2 Silverlight component";
            }
        }

        #endregion

        #region Events

        protected override void ga_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            string path = context.Request.Path.ToLower();

            if (path.Contains("/slideshowplugin-flickr-config.aspx"))
            {
                HandleConfigRequest(context);
                return;
            }
        }

        protected override void ga_RenderPostBody(StringBuilder sb, PostEventArgs e)
        {
            if (e.RenderLocation == PostRenderLocation.Web && e.Post.Name.ToLower() == PostName.ToLower())
            {
                Macros macros = new Macros();

                sb.AppendFormat("\n<object type=\"application/x-silverlight-2\" data=\"data:application/x-silverlight-2,\" width=\"{0}\" height=\"{1}\">\n", this.Width, this.Height);
                sb.AppendFormat("<param name=\"background\" value=\"{0}\" />\n", this.BackgroundColor);

                if (this.Theme == "XML")
                    sb.AppendFormat("<param name=\"initParams\" value=\"ConfigurationProvider=XmlConfigurationProvider;Path={0},DataProvider=FlickrDataProvider;UserName={1};ApiKey={2}\" />\n", VirtualPathUtility.ToAbsolute("~/slideshowplugin-flickr-config.aspx"), this.FlickrUsername, this.FlickrApiKey);
                else
                    sb.AppendFormat("<param name=\"initParams\" value=\"ConfigurationProvider={0},DataProvider=FlickrDataProvider;UserName={1};ApiKey={2}\" />\n", this.Theme, this.FlickrUsername, this.FlickrApiKey);

                sb.AppendFormat("<param name=\"source\" value=\"{0}\" />\n", macros.FullUrl(VirtualPathUtility.ToAbsolute("~/Vertigo.SlideShow.xap")));
                sb.Append(_resDownloadSilverlight);
                sb.Append("</object>\n");
            }
        }

        #endregion

        #region Configuration

        protected override FormElementCollection AddFormElements()
        {
            FormElementCollection fec = new FormElementCollection();

            fec.Add(new MessageElement("intro", "<strong>Flickr Slide.Show Photo Gallery</strong>",
                "Your Flickr sets will be shown as albums. <a href=\"http://kevinharder.com/downloads/graffiti/slide-show-plugin/\">Click here</a> for more help &amp; info."));

            fec.Add(new TextFormElement("flickrUserName", "Flickr Screen Name", null));
            fec.Add(new TextFormElement("flickrApiKey", "Flickr API Key", "Leave this as the default if you do not have your own Flickr API Key"));

            AddBaseFormElements(fec);
            AddAboutFormElements(fec);

            return fec;
        }

        protected override NameValueCollection DataAsNameValueCollection()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc["flickrUserName"] = FlickrUsername;
            nvc["flickrApiKey"] = FlickrApiKey;

            AddBaseConfigValues(nvc);

            return nvc;
        }

        public override StatusType SetValues(System.Web.HttpContext context, NameValueCollection nvc)
        {
            if (string.IsNullOrEmpty(nvc["flickrUserName"]))
            {
                SetMessage(context, "You forgot to enter your Flickr screen name.");
                return StatusType.Error;
            }
            FlickrUsername = nvc["flickrUserName"];

            if (string.IsNullOrEmpty(nvc["flickrApiKey"]))
            {
                SetMessage(context, "You forgot to enter your Flickr API Key.");
                return StatusType.Error;
            }
            FlickrApiKey = nvc["flickrApiKey"];

            return SetBaseValues(context, nvc);
        }

        #endregion

    }
}
